﻿using System;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using Model.Content;
using Service.Content;
using Utils.Types;

namespace CMS.Areas.EasyUI.Controllers
{
    /// <summary>
    /// Article
    /// </summary>
    public class ContentController : BaseController
    {
        private readonly ArticleService _articleservice;
        private readonly CategoryService _categoryservice;
        private static int selectoption = 0;//锁定文章类型

        /// <summary>
        /// ContentController
        /// </summary>
        public ContentController(ArticleService articleservice, CategoryService categoryservice)
        {
            _articleservice = articleservice;
            _categoryservice = categoryservice;
        }

        /// <summary>
        /// Add
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// Edit
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            var model = _articleservice.GetEntityByValue<Article>(id);
            selectoption = (int)model.CategoryID;
            return View(model);
        }

        /// <summary>
        /// ListContent
        /// </summary>
        /// <returns></returns>
        public ActionResult ListContent()
        {
            int count;
            var list = _articleservice.GetPagerListSQL(new Article() { UserID = UserID, IsDelete = 0 }, "", 1, 10, out count);
            ViewBag.Count = count;
            return View(list);
        }

        /// <summary>
        /// ListContentJson
        /// </summary>
        /// <returns></returns>
        public JsonResult ListContentJson()
        {
            int index = HttpContext.Request.GetValueToInt("index");
            int pagesize = HttpContext.Request.GetValueToInt("pagesize") > 0 ? HttpContext.Request.GetValueToInt("pagesize") : 0;
            var categorytype = HttpContext.Request.GetValue("categorytype");
            var title = HttpContext.Request.GetValue("title");
            var list = _articleservice.GetListByObjectPager<Article>(new { UserID = UserID, IsDelete = 0, CategoryID = categorytype, Title = title }, index, pagesize);
            var count = _articleservice.CountsByObject<Article>(new { UserID = UserID, IsDelete = 0, CategoryID = categorytype, Title = title });//统计总数
            var json = from data in list select new { ArticleID = data.ArticleID, IsShow = data.IsShow, CreateTime = data.CreateTime.DateTimeToString("G"), Title = data.Title, Images = data.Images, Details = data.Details, PageViewCount = data.PageViewCount };
            return Json(new { code = 200, data = json, count = count }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Content
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Content(int id)
        {
            return View(_articleservice.GetEntityByValue<Article>(id));
        }

        /// <summary>
        /// DeleteAllArticle
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost, ValidateAntiForgeryToken]
        public JsonResult DeleteAllArticle(string ids)
        {
            return Json(_articleservice.UpdateListById(UserID, ids) ? new { code = 200, msg = "成功！" } : new { code = 401, msg = "失败！" });
        }

        /// <summary>
        /// DeleteArticle
        /// </summary>
        /// <param name="model"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        [HttpPost, ValidateAntiForgeryToken]
        public JsonResult OperateArticle(Model.Content.Article model, int flag)
        {
            switch (flag)
            {
                case 1: //显示
                    model.IsShow = 1;
                    break;
                case 2: //隐藏
                    model.IsShow = 0;
                    break;
                case 3: //置顶
                    model.Sorts = 0;
                    break;
                case 4: //删除
                    model.IsDelete = 1;
                    model.IsShow = 0;
                    break;
            }
            return Json(_articleservice.Update(model) ? new { code = 200, msg = "成功！" } : new { code = 401, msg = "失败！" });
        }

        /// <summary>
        /// Add
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, ValidateAntiForgeryToken, ValidateInput(false)]
        public JsonResult Add(Model.Content.Article model)
        {
            model.UserID = BaseUserInfo.UserID;
            model.IsDelete = 0;
            return Json(_articleservice.Insert(model) > 0 ? new { code = 200, msg = "成功！" } : new { code = 401, msg = "失败！" });
        }

        /// <summary>
        /// Edit
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, ValidateAntiForgeryToken, ValidateInput(false)]
        public JsonResult Edit(Model.Content.Article model)
        {
            model.UserID = BaseUserInfo.UserID;
            return Json(_articleservice.Update(model) ? new { code = 200, msg = "成功！" } : new { code = 401, msg = "失败！" });
        }

        /// <summary>
        /// 封面图
        /// </summary>
        [HttpPost, ValidateAntiForgeryToken]
        public JsonResult Images(HttpPostedFileBase file)
        {
            if (file == null)
                return Json(new { code = 402, msg = "文件错误！" });
            var filename = Server.MapPath("~/TitleImage/" + DateTime.Now.ToString("yyyy-MM-dd") + "/");
            var str = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".jpeg";
            if (!System.IO.Directory.Exists(filename))
                System.IO.Directory.CreateDirectory(filename);
            filename += str;
            file.SaveAs(filename);
            return Json(new { code = 200, msg = str });
        }

        /// <summary>
        /// 获取下拉分类
        /// </summary>
        /// <returns></returns>
        public JsonResult GetCategoryOption()
        {
            var list = _categoryservice.GetListByObject<Category>(new { UserID = UserID, IsDelete = 0 });
            var json = from a in list select new { id = a.CategoryID, text = a.CategoryName, selected = a.CategoryID == selectoption };
            return new JsonResult { Data = json.ToList(), JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
    }
}